module hope_init_mod

  use spark
  use params_mod
  use recon_mod
  use operators_mod
  use topo_mod
  use geog_data_mod

  implicit none

  private

  public hope_init_run
  public hope_init_final

  type(cubed_sphere_mesh_type), allocatable :: mesh
  type(geog_data_type), allocatable :: geog_data

contains

  subroutine hope_init_run(namelist, comm)

    character(*), intent(in) :: namelist
    integer, intent(in), optional :: comm

    allocate(mesh, geog_data)

    call fiona_init()
    call params_init(namelist)
    call cubed_sphere_process_init(comm)
    call cubed_sphere_process_decomp_domain(mesh, equiangular, 1.0d0, nc, nz, hwx=ext_width, hwy=ext_width, hwe=recBdy+2, neq=GaussQuadOrder, rw=recBdy)
    call mesh%set_ghost_points
    call mesh%set_proj
    call geog_data%init(mesh)
    call recon_init(mesh)
    call spatial_operator_init(mesh)
    call topo_read()
    call topo_regrid(mesh, geog_data)
    call topo_deriv(mesh, geog_data)

    call geog_data%array%create_dataset('geog', file_path='geog.nc')
    call geog_data%array%write('geog', time_in_seconds=0.0d0)

  end subroutine hope_init_run

  subroutine hope_init_final()

    deallocate(mesh, geog_data)

    call cubed_sphere_process_final()

  end subroutine hope_init_final

end module hope_init_mod
